package com.stanyung.javasample.reflect;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.junit.Test;

public class TypeSample {

	@Test
	public void testType() {
		// TypeReference<Person> typeRef = new TypeReference<Person>(); //错误写法
		TypeReference<Person> typeRef = new TypeReference<Person>() {
		}; // 正确写法
		System.out.println(typeRef);
	}

	private static class Person {
	}

}

class TypeReference<T> {

	private final Type type;

	protected TypeReference() {
		Type superClass = getClass().getGenericSuperclass();
		type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
	}

	public Type getType() {
		return type;
	}
}
